using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using NicoView.Model.Service;
using NicoView.Model.Service.Comments;
using NicoView.Model.Service.Events;
using NicoView.Model.Service.Lives;

using System;
using System.Threading;
using System.Threading.Tasks;
using NicoView.Model;

using NicoView.ViewModel.Message;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace NicoView.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class NicoViewMainViewViewModel : NicoViewViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public NicoViewMainViewViewModel()
        {

            this.title = "";
            this.loadCommand = null;


        }




        




        private string title;
        public string Title
        {
            get
            {
                return this.title;
            }

            set
            {
                Set(()=>Title,ref this.title, value);
            }
        }




        public async Task LoadAsync()
        {

            

            

            

            if (!NicoViewApplication.Current.IsLoaded)
            {
                
                NicoViewApplication.Current.Warning += (e) =>
                {
                    this.MessengerInstance.Send(new NotificationMessage(e.NotifyMessage));
                };
                NicoViewApplication.Current.NotSetAuth += args =>
                {
                    this.MessengerInstance.Send(new NicoViewSetAuthMessage(args));
                };

                NicoViewApplication.Current.SetRequest += args =>
                {
                    this.MessengerInstance.Send(new NicoViewSetAuthMessage(args));
                };

                NicoViewApplication.Current.AuthFailed += args =>
                {
                    this.MessengerInstance.Send(new NicoViewSetAuthMessage(args));
                };



                await this.InternalApplication.LoadAsync();
                
                
                
                if (!IsInDesignMode)
                {
                    await InternalApplication.Instance.AuthenticateAsync();
                    
                }
            }


            

            
           

        }


        private ICommand loadCommand;

        public ICommand LoadCommand
        {
            get
            {
                if (this.loadCommand == null)
                {
                    this.loadCommand = new RelayCommand(async() =>
                    {
                        await this.LoadAsync();
                    });
                }

                return this.loadCommand;
            }
        }
       


        



        







    }
}